//
//  XLTabBar.swift
//  微博
//
//  Created by macbook on 16/9/29.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

//协议 switf中协议需要遵守NSObjectProtocol协议
protocol XLTabBarDelegate: NSObjectProtocol{

    //声明协议方法
    func composeBtnClickPushController()
    
}


class XLTabBar: UITabBar {
    
    //声明代理
    weak var XLTabbarDelegate: XLTabBarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
          //设置宽度
        let winth = KScreenW / 5
        //记录标识
        var index:CGFloat = 0
        //通过遍历得到子控件
        for varlue in self.subviews {
            
            if varlue.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
              //设置frame
                varlue.frame.size.width = winth;
                varlue.frame.origin.x = winth * index;
                index++;
                //给第三个留出位置
                if index == 2 {
                    index++
                }
            }
        }
        //设置中间按钮位置
        composeBtn.center.x = self.frame.size.width * 0.5
        composeBtn.center.y = self.frame.size.height * 0.5
  
    }
    
    
    //设置视图
    func setupUI(){
    
        addSubview(composeBtn)
    }
    
    //懒加载创建按钮
    private lazy var composeBtn : UIButton = {
    
        let btn = UIButton()
        //设置背景图片及选中图片
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: .Normal);
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: .Highlighted);
        btn.sizeToFit()
        
        //添加方法
        btn.addTarget(self, action: #selector(XLTabBar.btnClick), forControlEvents: .TouchUpInside)
        
        return btn
    
    }()
    
    //按钮的点击方法
   @objc private func btnClick(){
 
    XLTabbarDelegate?.composeBtnClickPushController()
    
    }
    
    
}











